package com.sgb.trip.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.view.TimePickerView;
import com.luck.picture.lib.tools.AnimUtils;
import com.sgb.link.entity.trip.AirportCityEntity;
import com.sgb.link.entity.trip.FlightSearchEntity;
import com.sgb.link.router.PathTrip;
import com.sgb.link.view.fragment.AppLazyFragment;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripFragmentTripHomeBinding;
import com.sgb.trip.view.activity.CitySelectionActivity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickerHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Time:2021/9/8
 * Author:黄淋
 * Description:食住行首页
 */
public class TripHomeFragment extends AppLazyFragment {
    TripFragmentTripHomeBinding mBinding;
    private int requestCodeStart = 201;
    private int requestCodeArrive = 202;
    private TimePickerView mTimePickerStart;
    private TimePickerView mTimePickerBack;
    FlightSearchEntity mEntity = new FlightSearchEntity();

    public static TripHomeFragment newInstance() {
        TripHomeFragment fragment = new TripHomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.trip_fragment_trip_home, container, false);
        mBinding.tvFlightSingle.setSelected(true);
        mBinding.setOnClick(this);
        return mBinding.getRoot();
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();//火车
        if (id == R.id.type_train || id == R.id.type_hotel) {//酒店
            MToast.showToast(getContext(), "功能开发中，敬请期待");
        } else if (id == R.id.tv_flight_single) {//单程
            if (!mBinding.tvFlightSingle.isSelected()) {
                mBinding.tvFlightSingle.setSelected(true);
                mBinding.tvFlightReturn.setSelected(false);
                mBinding.tvChooseDateArrive.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(mEntity.getReturnDate())) {
                    mEntity.setReturnDate("");
                }
            }
        } else if (id == R.id.tv_flight_return) {//往返
            if (!mBinding.tvFlightReturn.isSelected()) {
                mBinding.tvFlightReturn.setSelected(true);
                mBinding.tvFlightSingle.setSelected(false);
                mBinding.tvChooseDateArrive.setVisibility(View.VISIBLE);
                if (!mBinding.tvChooseDateArrive.getText().toString().equals("返回日期")) {
                    mEntity.setReturnDate(mBinding.tvChooseDateArrive.getText().toString());
                }
            }
        } else if (id == R.id.tv_choose_city_start) {//出发城市
            Intent intent = new Intent();
            intent.setClass(getActivity(), CitySelectionActivity.class);
            startActivityForResult(intent, requestCodeStart);
        } else if (id == R.id.tv_choose_city_arrive) {//到达城市
            Intent intent1 = new Intent();
            intent1.setClass(getActivity(), CitySelectionActivity.class);
            startActivityForResult(intent1, requestCodeArrive);
        } else if (id == R.id.iv_change_city) {//交换城市
            if (mEntity == null || TextUtils.isEmpty(mEntity.getStartCity()) || TextUtils.isEmpty(mEntity.getArriveCity())) {
                MToast.showToast(getContext(), "请完善城市信息");
                return;
            }
            //创建旋转动画
            AnimUtils.rotateArrow(mBinding.ivChangeCity, false);

            mBinding.tvChooseCityStart.setText(mEntity.getArriveCity());
            mBinding.tvChooseCityArrive.setText(mEntity.getStartCity());
            mEntity.setStartCity(mBinding.tvChooseCityStart.getText().toString());
            mEntity.setArriveCity(mBinding.tvChooseCityArrive.getText().toString());
            String startCityNo = mEntity.getStartCityNo();
            mEntity.setStartCityNo(mEntity.getArriveCityNo());
            mEntity.setArriveCityNo(startCityNo);
        } else if (id == R.id.tv_choose_date_start) {//出发日期
            //获取时间加一年
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, 1);//增加一年
            mTimePickerStart = PickerHelper.with(getContext()).getCommonTimeBuild((date, view) -> {
                String dateFormat = DateUtils.timestampToDate(date.getTime());
                mBinding.tvChooseDateStart.setText(dateFormat);
                mBinding.tvChooseDateStart.setTextColor(getResources().getColor(R.color.black));
                mEntity.setStartDate(dateFormat);
                mEntity.setStartDateStamp(date.getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("E");
                String week = sdf.format(date);
                if (week.contains("周"))
                    week = week.replace("周", "");
                mEntity.setStartWeek(week);
                if (mEntity.getReturnDateStamp() != 0 && date.getTime() > mEntity.getReturnDateStamp()) {
                    mEntity.setReturnDateStamp(0);
                    mEntity.setReturnDate("");
                    mBinding.tvChooseDateArrive.setText("返回日期");
                    mBinding.tvChooseDateArrive.setTextColor(getResources().getColor(R.color.color_C0C4CC));
                }
            }).setLineSpacingMultiplier(2.0f)
                    .setRangDate(Calendar.getInstance(), cal)
                    .build();
            mTimePickerStart.setTitleText("选择出发日期");
            if (mEntity.getStartDateStamp() != 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(mEntity.getStartDateStamp());
                mTimePickerStart.setDate(calendar);
            }
            mTimePickerStart.show();
        } else if (id == R.id.tv_choose_date_arrive) {//返回日期
            //获取时间加一年
            Calendar cal1 = Calendar.getInstance();
            cal1.add(Calendar.YEAR, 1);//增加一年

            Calendar calStart = Calendar.getInstance();
            if (mEntity.getStartDateStamp() != 0) {
                calStart.setTimeInMillis(mEntity.getStartDateStamp());
            }
            mTimePickerBack = PickerHelper.with(getContext()).getCommonTimeBuild((date, view) -> {
                String dateFormat = DateUtils.timestampToDate(date.getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("E");
                String week = sdf.format(date);
                if (week.contains("周"))
                    week = week.replace("周", "");
                mBinding.tvChooseDateArrive.setText(dateFormat);
                mBinding.tvChooseDateArrive.setTextColor(getResources().getColor(R.color.black));
                mEntity.setReturnDate(dateFormat);
                mEntity.setReturnDateStamp(date.getTime());
                mEntity.setBackWeek(week);
            }).setLineSpacingMultiplier(2.0f)
                    .setRangDate(mEntity.getStartDateStamp() != 0 ? calStart : Calendar.getInstance(), cal1)
                    .build();
            mTimePickerBack.setTitleText("选择返回日期");
            if (mEntity.getReturnDateStamp() != 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(mEntity.getReturnDateStamp());
                mTimePickerBack.setDate(calendar);
            }
            mTimePickerBack.show();
        } else if (id == R.id.tv_search_flight) {//搜索
            if (mEntity != null) {
                if (TextUtils.isEmpty(mEntity.getStartCity())) {
                    MToast.showToast(getContext(), "请填写出发城市");
                    return;
                }
                if (TextUtils.isEmpty(mEntity.getArriveCity())) {
                    MToast.showToast(getContext(), "请填写到达城市");
                    return;
                }
                if (TextUtils.isEmpty(mEntity.getStartDate())) {
                    MToast.showToast(getContext(), "请填写出发日期");
                    return;
                }
                if (mBinding.tvFlightReturn.isSelected()) {
                    if (TextUtils.isEmpty(mEntity.getReturnDate())) {
                        MToast.showToast(getContext(), "请填写返回日期");
                        return;
                    }
                    // TODO: 2021/9/10 跳转往返航班选择
                    mEntity.setSingle(false);
                    ARouter.getInstance().build(PathTrip.FlightSearchActivity).withSerializable(RouterKey.DATA, mEntity).navigation();
                } else {
                    mEntity.setSingle(true);
                    ARouter.getInstance().build(PathTrip.FlightSearchActivity).withSerializable(RouterKey.DATA, mEntity).navigation();
                }
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == 200) {
            AirportCityEntity aMapAddressEntity = (AirportCityEntity) data.getExtras().get(RouterKey.DATA);
            if (requestCode == requestCodeStart) {
                if (!TextUtils.isEmpty(mEntity.getArriveCityNo()) && mEntity.getArriveCityNo().equals(aMapAddressEntity.getCityCode())) {
                    MToast.showToast(getContext(), "不能选择相同城市");
                    return;
                }
                mBinding.tvChooseCityStart.setText(aMapAddressEntity.getCityName());
                mBinding.tvChooseCityStart.setTextColor(getResources().getColor(R.color.black));
                mEntity.setStartCity(aMapAddressEntity.getCityName());
                mEntity.setStartCityNo(aMapAddressEntity.getCityCode());
            } else if (requestCode == requestCodeArrive) {
                if (!TextUtils.isEmpty(mEntity.getStartCityNo()) && mEntity.getStartCityNo().equals(aMapAddressEntity.getCityCode())) {
                    MToast.showToast(getContext(), "不能选择相同城市");
                    return;
                }
                mBinding.tvChooseCityArrive.setText(aMapAddressEntity.getCityName());
                mBinding.tvChooseCityArrive.setTextColor(getResources().getColor(R.color.black));
                mEntity.setArriveCity(aMapAddressEntity.getCityName());
                mEntity.setArriveCityNo(aMapAddressEntity.getCityCode());
            }
        }
    }
}
